package Team.Yaohuo.utils;

import Team.Yaohuo.config.ThreadLocalConfig;
import Team.Yaohuo.constant.JDYConstant;
//import Team.Yaohuo.listener.RabbitListenerWait;
import Team.Yaohuo.properties.BaiduProperties;
import Team.Yaohuo.properties.WxProperties;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 消息推送
 */
@Component
@Slf4j
@Data
public class MegPushUtil {

    @Autowired
    private WxProperties wxProperties;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private HttpServletRequest httpServletRequest;
    @Autowired
    private BaiduProperties baiduProperties;
    @Autowired
    private WxUtil wxUtil;
//    @Autowired
//    private RabbitListenerWait rabbitListenerWait;

    private static final String MEG_PUSH_URL = "https://qyapi.weixin.qq.com/cgi-bin/message/send";

    private Integer count = 0;

    public void yxMes(String userId, String accsess_token) {
        String date = DateTime.now().toString("yyyy年MM月dd日");
        String ipAddr = httpServletRequest.getRemoteAddr();
        // 调用百度地图获取当前用户ip的地理位置
        String baidu_url = "https://api.map.baidu.com/location/ip?ip=" + ipAddr + "&coor=bd09ll&ak=" + baiduProperties.getAK();
        ResponseEntity<Map> exchange = restTemplate.exchange(baidu_url, HttpMethod.GET, null, Map.class);
        Map content = (Map) exchange.getBody().get("content");
        String address = (String) content.get("address");

        // 方案一文本类型
        Map<String, Object> meg = wxUtil.messagePushTextType(userId, accsess_token, date, ipAddr, address);
        HttpEntity<Map<String, Object>> mapHttpEntity = new HttpEntity<>(meg);
        ResponseEntity<Map> responseEntity = restTemplate.exchange(MEG_PUSH_URL + "?access_token=" + accsess_token, HttpMethod.POST, mapHttpEntity, Map.class);
        if (responseEntity.getBody() != null) {
            Integer errcode = (Integer) responseEntity.getBody().get("errcode");
            String msgid = (String) responseEntity.getBody().get("msgid");
            if (errcode == 0) {
                log.info("消息推送成功，当前megId为:{}", msgid);
            } else {
                log.error("消息推送失败，返回错误码为:{}, 请前往https://developer.work.weixin.qq.com/document/path/96213进行查看", errcode);
            }
        }
        ThreadLocalConfig.setCurrentId(userId);
        // 获取当前用户的简道云消息待办
        Map<String, Object> jdyMapWait = new HashMap<>();
        jdyMapWait.put("username", userId);
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Authorization", "Bearer " + JDYConstant.API_KEY);
        HttpEntity<Object> httpEntity = new HttpEntity<>(jdyMapWait, httpHeaders);
        ResponseEntity<Map> waitMessage = restTemplate.exchange(JDYConstant.PENDING_TASKS_IN_JIANDAOYUN, HttpMethod.POST, httpEntity, Map.class);
        List<Map> tasks = (List) waitMessage.getBody().get("tasks");
        log.info("当前有：{}条代办", tasks.size());

        Map<String, Object> waitMapJdy = new HashMap<>();

        // 当没有代办时
        if (tasks.size() == 0) {
            waitMapJdy.put("form_title", "无");
            waitMapJdy.put("url", "无");
        }else {
            for (int i = 0; i < 1; i++) {
                // 代办名称
                String formTitle = (String) tasks.get(i).get("form_title");
                // 代办网页地址
                String waitUrl = (String) tasks.get(i).get("url");
                waitMapJdy.put("form_title", formTitle);
                waitMapJdy.put("url", waitUrl);
            }
        }
        // 简道云消息代办推送
        Map<String, Object> waitMapJdyMap = wxUtil.messagePushTextTypeWaitJDY(userId, waitMapJdy, tasks.size() + "");
        HttpEntity<Map<String, Object>> http = new HttpEntity<>(waitMapJdyMap);
        ResponseEntity<Map> response = restTemplate.exchange(MEG_PUSH_URL + "?access_token=" + accsess_token, HttpMethod.POST, http, Map.class);
        if (response.getBody() != null) {
            Integer errcode = (Integer) response.getBody().get("errcode");
            String msgid = (String) response.getBody().get("msgid");
            if (errcode == 0) {
                log.info("消息推送成功，当前megId为:{}", msgid);
            } else {
                log.error("消息推送失败，返回错误码为:{}, 请前往https://developer.work.weixin.qq.com/document/path/96213进行查看", errcode);
            }
        }
    }
}

